﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MediaImporter.Controllers
{
    class ImportController
    {
        IMainView view;
        BackgroundCopyController copyJpeg;
        BackgroundCopyController copyVideo;
        public event RunWorkerCompletedEventHandler Done;

        public ImportController(IMainView view)
        {
            this.view = view;
            copyJpeg = new BackgroundCopyController(view.ProgressControl);
            copyJpeg.Done += copyJpeg_Done;
            copyVideo = new BackgroundCopyController(view.ProgressControl);
            copyVideo.Done += copyVideo_Done;
            var date = DateTime.Now;
        }

        void copyVideo_Done(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            if (Done != null)
                Done(this, e);
        }

        void copyJpeg_Done(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled || e.Error != null)
            {
                if (Done != null)
                    Done(this, e);
                return;
            }
            StartCopyVideo();
        }

        private void StartCopyVideo()
        {
            var files = Directory.GetFiles(view.SourceFolder, "*.mov");
            copyVideo.StartCopyFiles(view.DestinationFolder + "-Video", files, "Importing video.");
        }

        public void StartImport()
        {
            if (string.IsNullOrWhiteSpace(view.SourceFolder) || string.IsNullOrWhiteSpace(view.DestinationFolder))
                return;

            try
            {
                var files = Directory.GetFiles(view.SourceFolder, "*.jpg");
                copyJpeg.StartCopyFiles(view.DestinationFolder, files, "Importing jpeg files.");
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
